package com.hispot.download;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;

/**
 * Created by ASUS-PC on 2016/11/13.
 */

public class FileDownloader {
    public static Context CONTEXT;

    private static FileDownloadManager downloadManager;

    private FileDownloader() {
    }

    public static void init(Context context) {
        init(context, new Builder()
                .setDownloadDir(FileDownloadConstant.DEFAULT_DOWNLOAD_DIR)
                .setOkHttpClient(new OkHttpClient.Builder().build())
                .setThreadCount(FileDownloadConstant.THREAD_COUNT));
    }

    public static void init(Context context, Builder builder) {
        CONTEXT = context;
        builder.build();
    }

    public static void start(String downloadUrl) {
        downloadManager.start(downloadUrl,FileDownloadUtils.parseMD5(downloadUrl)+".apk");
    }

    public static void start(String downloadUrl, String fileName) {
        downloadManager.start(downloadUrl,fileName);
    }

    public static void pause(String downloadUrl){
        downloadManager.pause(downloadUrl);
    }

    public synchronized static FileDownloadModel getFileDownloadStatus(String downloadUrl){
        return downloadManager.getFileDownloadStatus(downloadUrl);
    }

    public static void setDownloadListener(FileDownloadListener downloadListener) {
        downloadManager.setDownloadListener(downloadListener);
    }

    public static void delete(String downloadUrl) {
        downloadManager.delete(downloadUrl);
    }

    public static int delete(int status) {
        return downloadManager.delete(status);
    }

    public static void close(){
        downloadManager.close();
    }

    public static void deleteAll(){
        downloadManager.deleteAll();
    }

    public static String getDownloadDir(){
        return downloadManager.getDownloadDir();
    }

    public static class Builder {

        private String downloadDir;
        private int threadCount;
        private OkHttpClient okHttpClient;

        public Builder setDownloadDir(String downloadDir) {
            this.downloadDir = downloadDir;
            return this;
        }

        public Builder setThreadCount(int threadCount) {
            this.threadCount = threadCount;
            return this;
        }

        public Builder setOkHttpClient(OkHttpClient okHttpClient) {
            this.okHttpClient = okHttpClient;
            return this;
        }

        void build() {
            downloadManager = new FileDownloadManager(okHttpClient, downloadDir, threadCount);
        }
    }
}
